XML Code Generator

When woodworkers are faced with the task of producing the same thing over and over, they cheat. They build themselves a jig or template. If they get the jig right once, they can reproduce a piece of work time after time. The jig takes away complexity and reduces the chances of making mistakes, leaving the craftsman free to concentrate on quality.

Andrew Hunt and David Thomas, The Pragmatic Programmer, p. 102.

Java Dwarf Manager contains numerous examples of repeated information suitable for code generation. A Python script can read a XML description of a class and generate both JavaBean class source and Java code for database access. The XML can be validated using a XML DTD. The code generation can be done either entirely inside the script or use template substitution.

Code developed on Mac OS X using Python 2.2, which includes built-in XML support (added xmlproc from pyxml for optional DTD validation).

For more code generation, see Ruby Code Generator which is used to generate EJB Entity Beans and associated XML deployment descriptors from the SQL schema.

Props to Jack Herrington, who first showed me these code generation techniques (in Perl ;-). Code generation is not a new idea; there are over 400 Open Source code generator projects on Sourceforge.